Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add get_placeholder to handle automatic check constraint evaluation #125

Merged
merged 1 commit into from
Dec 13, 2023

Conversation

sevdog
Copy link
Contributor

@sevdog sevdog commented Dec 12, 2023

As from django 4.1 CheckConstraint are evaluated during the Model.full_clean process using model data.

As of now this does not work with netfields since the checker does not produce the right query (see https://github.com/sevdog/netfields-check-error for reference).
With a check constraint like the following:

CheckConstraint(check=Q(network__net_contains=F('inet')),name='inet_contained')

the ORM produces the following query:

SELECT 1 AS "_check" WHERE COALESCE(('192.168.0.0/24' >> ('192.168.0.1')), true)

which raises an error on Postgresql since

operator is not unique: unknown >> unknown [...]
HINT: Could not choose a best candidate operator. You might need to add explicit type casts

Thus if this is called inside a transaction then any following query will fail since the transaction is not on a clean state and raising

current transaction is aborted, commands ignored until end of transaction block

Looking at similar issue on django (see https://code.djangoproject.com/ticket/34059) the solution seems to be adding a new method Field.get_placeholder which is used by ORM to insert values in this process.

@jimfunk jimfunk merged commit 755aa86 into jimfunk:master Dec 13, 2023
30 of 31 checks passed
@sevdog sevdog deleted the placeholder-for-checks branch December 14, 2023 07:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants